<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Order;
use yii;


class OrderLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getOrder($order_type,$query = [], $page = 1, $limit = 10)
    {
        $start = $end = false;
        $query_temp = $query;
        if(isset($query['start_time']) && $query['start_time']!=''){
            $start_time_query = "order_time>='".$query['start_time']." 00:00:00'";
            $start = $query['start_time'];
            unset($query['start_time']);
            $start = true;
        }
        if(isset($query['end_time'])  && $query['end_time']!=''){
            $end_time_query = "order_time<='".$query['end_time']." 23:59:59'";
            $end = $query['end_time'];
            unset($query['end_time']);
            $end = true;
        }
        if(isset($query['user_status'])  && $query['user_status']!=-1){
            if($order_type){
                $order = Order::find()
                    ->select('o.*,c.course_name,u.nick_name,u.user_status')
                    ->from('{{%order}} o')
                    ->leftJoin('{{%course}} c','c.course_id=o.course_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->filterWhere($query)->andWhere("order_type=$order_type");
            }else{
                $order = Order::find()
                    ->select('o.*,c.chapter_name,u.nick_name,u.user_status')
                    ->from('{{%order}} o')
                    ->leftJoin('{{%chapter}} c','c.chapter_id=o.chapter_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->filterWhere($query)->andWhere("order_type=$order_type");
            }
        }else{
            if(isset($query['user_status'])){
                $user_status = $query['user_status'];
                unset($query['user_status']);
            }
            if($order_type){
                $order = Order::find()
                    ->select('o.*,c.course_name,u.nick_name,u.user_status')
                    ->from('{{%order}} o')
                    ->leftJoin('{{%course}} c','c.course_id=o.course_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->filterWhere($query)->andWhere("order_type=$order_type");
            }else{
                $order = Order::find()
                    ->select('o.*,c.chapter_name,u.nick_name,u.user_status')
                    ->from('{{%order}} o')
                    ->leftJoin('{{%chapter}} c','c.chapter_id=o.chapter_id')
                    ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                    ->filterWhere($query)->andWhere("order_type=$order_type");
            }
        }


        if($start)
            $order->andWhere($start_time_query);
        if($end)
            $order->andWhere($end_time_query);
        if(isset($user_status)){
            $query['user_status'] = $user_status;
        }
        if(isset($start)){
            $query['start_time'] = $start;
        }
        if(isset($end)){
            $query['end_time'] = $end;
        }


        $order->offset(($page - 1) * $limit)->limit($limit);
        $lists = $order->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $order->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/index',
                'query' => $query_temp
            ]
        ];
        return $return;
    }





}